import {
  getfang_office,
  getfang_rental,
  getfang_office_rental,
  getfang_shops_rental,
} from '@/api/business';

const IndexModel = {
  namespace: 'office',

  state: {
    list: JSON.parse(localStorage.getItem('officeList'))
      ? JSON.parse(localStorage.getItem('officeList'))
      : [],
    detailsList: {},
  },

  effects: {
    *officeinit(_, { call, put }) {
      const data = yield call(getfang_office);
      yield put({
        type: 'GET_OFFICELIST',
        list: data.data.RECORDS,
      });
    },
    *rentalinit(_, { call, put }) {
      const data = yield call(getfang_rental);
      yield put({
        type: 'GET_RENTALLIST',
        list: data.data.RECORDS,
      });
    },
    *shopofficeinit(_, { call, put }) {
      const data = yield call(getfang_office_rental);
      yield put({
        type: 'GET_SHOPOFFICELIST',
        list: data.data.RECORDS,
      });
    },
    *shoprentalinit(_, { call, put }) {
      const data = yield call(getfang_shops_rental);
      yield put({
        type: 'GET_SHOPRENTALLIST',
        list: data.data.RECORDS,
      });
    },
  },
  reducers: {
    GET_OFFICELIST(state, action) {
      state.list = action.list;
      localStorage.setItem('officeList', JSON.stringify(state.list));
      return {
        ...state,
      };
    },
    GET_RENTALLIST(state, action) {
      state.list = action.list;
      localStorage.setItem('officeList', JSON.stringify(state.list));
      return {
        ...state,
      };
    },
    GET_SHOPOFFICELIST(state, action) {
      state.list = action.list;
      localStorage.setItem('officeList', JSON.stringify(state.list));
      return {
        ...state,
      };
    },
    GET_SHOPRENTALLIST(state, action) {
      state.list = action.list;
      localStorage.setItem('officeList', JSON.stringify(state.list));
      return {
        ...state,
      };
    },
    GET_DETAILSLIST(state, action) {
      JSON.parse(localStorage.getItem('officeList')).forEach((item, index) => {
        if (item.id == action.payload) {
          state.detailsList = item;
        }
      });
      return {
        ...state,
      };
    },
    del(state, action) {
      console.log(action);
      state.list.forEach((item, index) => {
        if (item.id == action.payload.id) {
          state.list.splice(index, 1);
          localStorage.setItem('officeList', JSON.stringify(state.list));
        }
      });
      return {
        ...state,
      };
    },
    GET_DETAILSLIST(state, action) {
      JSON.parse(localStorage.getItem('officeList')).forEach((item, index) => {
        if (item.id == action.payload) {
          state.detailsList = item;
        }
      });
      return {
        ...state,
      };
    },
    CHANGE_LIST(state, action) {
      console.log(action);
      state.list.forEach((item, index) => {
        if (item.id == action.payload.id) {
          state.list[index] = action.payload;
          console.log(state.list);
          localStorage.setItem('officeList', JSON.stringify(state.list));
        }
      });
      return {
        ...state,
      };
    },
    ADD_LIST(state, action) {
      console.log(action, '1111111111111111');
      state.list.push(action.payload);
      localStorage.setItem('officeList', JSON.stringify(state.list));
      return {
        ...state,
      };
    },
  },
};

export default IndexModel;
